Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
The find-up npm package is a utility that allows you to find and read files or directories by traversing up the file system. It's useful for finding configuration files and other resources that may be located in a parent directory relative to the current working directory.
Find a file by name
This feature allows you to find a file by its name, starting from the current directory and searching upwards through the parent directories.
const findUp = require('find-up');
(async () => {
const filePath = await findUp('unicorn.png');
console.log(filePath);
//=> '/Users/sindresorhus/unicorn.png'
})();
Find a file using a matcher function
This feature allows you to use a custom matcher function to find a file. The function receives the current directory and returns the path to stop at or `undefined` to continue searching.
const findUp = require('find-up');
(async () => {
const filePath = await findUp(directory => directory === '/Users/sindresorhus' ? 'unicorn.png' : undefined);
console.log(filePath);
//=> '/Users/sindresorhus/unicorn.png'
})();
Find a file with a specific name in an array of names
This feature allows you to pass an array of file names to `findUp`, and it will return the first file found with one of those names.
const findUp = require('find-up');
(async () => {
const filePath = await findUp(['rainbow.png', 'unicorn.png']);
console.log(filePath);
//=> '/Users/sindresorhus/unicorn.png'
})();
The locate-path package is similar to find-up as it also searches for files or directories by traversing up the directory tree. However, locate-path does not provide the convenience methods for matching files that find-up does.
The pkg-up package is designed specifically to find the closest package.json file in the directory tree. It is a more specialized tool compared to find-up, which can search for any file or directory.
findup-sync is similar to find-up but uses glob patterns for searching and is based on the micromatch library. It provides a synchronous API, unlike find-up which is promise-based and supports async/await.
Find a file or directory by walking up parent directories
$ npm install find-up
/
└── Users
└── sindresorhus
├── unicorn.png
└── foo
└── bar
├── baz
└── example.js
example.js
const findUp = require('find-up');
(async () => {
console.log(await findUp('unicorn.png'));
//=> '/Users/sindresorhus/unicorn.png'
console.log(await findUp(['rainbow.png', 'unicorn.png']));
//=> '/Users/sindresorhus/unicorn.png'
})();
Returns a Promise
for either the filepath or null
if it couldn't be found.
Returns a Promise
for either the first filepath found (by respecting the order) or null
if none could be found.
Returns a filepath or null
.
Returns the first filepath found (by respecting the order) or null
.
Type: string
Filename of the file to find.
Type: Object
Type: string
Default: process.cwd()
Directory to start from.
require.resolve()
but from a given pathMIT © Sindre Sorhus
FAQs
Find a file or directory by walking up parent directories
We found that find-up demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.